<?php

namespace common\models\work;

use Yii;
use common\helpers\StringHelper;

/**
 * This is the model class for table "{{%resident}}".
 *
 * @property int $id 居民ID
 * @property int $user_id 用户ID
 * @property string $realname 姓名
 * @property string $mobile 手机号
 * @property string $mobile_hash 加密手机号
 * @property string $idcardno 身份证号
 * @property string $idcardno_hash 加密身份证号
 * @property int $village_id 社区ID
 * @property int $family_id 家庭ID
 * @property int $round_id 轮次ID
 * @property string $address 联系地址
 * @property int $status 状态
 * @property int $created_at 注册时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 注销时间
 */
class Resident extends \common\db\ActiveRecord {

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%resident}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'village_id', 'family_id', 'round_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['realname'], 'required'],
            [['realname'], 'string', 'max' => 50],
            [['mobile', 'idcardno'], 'string', 'max' => 32],
            [['mobile_hash', 'idcardno_hash', 'address'], 'string', 'max' => 255],
            [['idcardno'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => '居民ID',
            'user_id' => '用户ID',
            'realname' => '姓名',
            'mobile' => '手机号',
            'mobile_hash' => '加密手机号',
            'idcardno' => '身份证号',
            'idcardno_hash' => '加密身份证号',
            'village_id' => '社区ID',
            'family_id' => '家庭ID',
            'address' => '联系地址',
            'round_id' => '轮次ID',
            'status' => '状态',
            'created_at' => '注册时间',
            'updated_at' => '更新时间',
            'deleted_at' => '注销时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getRound() {
        return $this->hasOne(SurveyRound::class, ['id' => 'round_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getSurvey() {
        return $this->hasOne(Survey::class, ['round_id' => 'round_id']);
    }

    /**
     * 根据身份证号查询用户信息
     * @param string $idcardno
     * @return static|null
     */
    public static function findByIdcardNo($idcardno) {
        return static::findOne([
            'idcardno' => $idcardno,
        ]);
        $entra = Yii::$app->security->generatePrivateKey($idcardno);
        return static::findOne([
            'idcardno' => $entra,
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getMaskMobile() {
        return StringHelper::maskChar($this->mobile);
    }

    /**
     * {@inheritdoc}
     */
    public function getMaskIdcardno() {
        return StringHelper::maskChar($this->idcardno, 6, 4);
    }

}
